import instance from "./axios";
import { getLocalUserToken } from "@/utils";
import { BasicApiResponse } from '@/models/basicType'

export function AppGet(url:string, data:any ={})
{
    return new Promise((resolve:any, reject:any)=>{
        instance.get(url, { params : data })
        .then((res)=>{
            if(res.data.status ===0 || res.data.status === 200){
                resolve(res.data.data)
            }
            else{
                // TODO：确认一下是否正确
                reject(res.data)
            }
        })
        .catch(()=>{
            reject("服务器错误，请重试")
        })
    })
}

export function AppPost(url:string, data:any ={}, config:any ={})
{
    const allConfig = Object.assign({
        headers: {
            "Access-Token": getLocalUserToken(),
        }
    }, config);
    return new Promise((resolve : any,reject : any)=>{
        instance.post(url, data, allConfig)
        .then((res)=>{
            if(res.data.status ===0 || res.data.status === 200){
                resolve(res.data.data)
            }
            else{
                //TODO：确认一下是否正确
                reject(res.data)
            }
        })
        .catch(()=>{
            reject("服务器错误，请重试")
        })
    })
}

//////////////////////Generic version //////////////////////////////////////
export function AppPostT<T>(url:string, data:any ={}, config:any ={}):Promise<BasicApiResponse<T>>
{
    const allConfig = Object.assign({
        headers: {
            "Access-Token": getLocalUserToken(),
        }
    }, config);
    return new Promise((resolve : any,reject : any)=>{
        instance.post<BasicApiResponse<T>>(url, data, allConfig)
        .then((res)=>{
            if(res.status === 200){
                resolve(res.data)
            }
            else{
                //TODO：确认一下是否正确,可能转相应 Reject 页面
                // reject(res.data)
            }
        })
        .catch(()=>{
            //TODO：确认一下是否正确,可能转相应 Reject 页面
            //reject("服务器错误，请重试")
        })
    })
}